{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Invert a Binary Tree\n",
    "\n",
    "[The Original Question](https://mp.weixin.qq.com/s/zXQgH7afhBPQSrCjiAMsrA)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "You are given the root of a binary tree. Invert the binary tree in place. That is, all left children should become right children, and all right children should become left children."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "```text\n",
    "     a\n",
    "   /   \\\n",
    "  b     c\n",
    " / \\   /\n",
    "d   e f\n",
    "```\n",
    "\n",
    "The inverted version of this tree is as follows:\n",
    "\n",
    "```text\n",
    "   a\n",
    " /   \\\n",
    "c     b\n",
    " \\   / \\\n",
    "  f e   d\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Node structure\n",
    "class Node:\n",
    "    def __init__(self, value, left_child=None, right_child=None):\n",
    "        self.value = value\n",
    "        self.L = left_child\n",
    "        self.R = right_child"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Reverse a binary tree recursively\n",
    "def reverse(head):\n",
    "    if head is None:\n",
    "        return\n",
    "    (head.L, head.R) = (head.R, head.L)\n",
    "    reverse(head.L)\n",
    "    reverse(head.R)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Inorder Traversal (NOT recommended)\n",
    "# When a binary tree has been reversed,\n",
    "# the results of LDR will also be reversed.\n",
    "def LDR(head):\n",
    "    nodes = []\n",
    "    if head is None:\n",
    "        pass\n",
    "    else:\n",
    "        if head.L is not None:\n",
    "            nodes.append(LDR(head.L))\n",
    "        nodes.append(head.value)\n",
    "        if head.R is not None:\n",
    "            nodes.append(LDR(head.R))\n",
    "    return ' -> '.join(nodes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "d -> b -> e -> a -> f -> c\n",
      "c -> f -> a -> e -> b -> d\n"
     ]
    }
   ],
   "source": [
    "head = Node('a', Node('b', Node('d'), Node('e')), Node('c', Node('f'), None))\n",
    "print(LDR(head))\n",
    "reverse(head)\n",
    "print(LDR(head))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
